package me.zhengjie.dailyPoem.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.dailyPoem.domain.DailyPoemDiscussion;
import me.zhengjie.dailyPoem.service.DailyPoemDiscussionService;
import me.zhengjie.dailyPoem.domain.vo.DailyPoemDiscussionQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

@RestController
@RequiredArgsConstructor
@Api(tags = "每日一诗-讨论贴")
@RequestMapping("/api/daily_poem/discussion")
public class DailyPoemDiscussionController {

    private final DailyPoemDiscussionService dailyPoemDiscussionService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportDailyPoemDiscussion(HttpServletResponse response, DailyPoemDiscussionQueryCriteria criteria) throws IOException {
        List<DailyPoemDiscussion> list = dailyPoemDiscussionService.queryAll(criteria);
        dailyPoemDiscussionService.download(list, response);
    }

    @GetMapping
    @Log("查询讨论")
    @ApiOperation("查询讨论")
    @AnonymousAccess
    public ResponseEntity<PageResult<DailyPoemDiscussion>> queryDailyPoemDiscussion(DailyPoemDiscussionQueryCriteria criteria, Page<Object> page){
        PageResult<DailyPoemDiscussion> result = dailyPoemDiscussionService.queryAll(criteria, page);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping
    @Log("新增讨论")
    @ApiOperation("新增讨论")
    @AnonymousAccess
    public Integer createDailyPoemDiscussion(@Validated @RequestBody DailyPoemDiscussion resources){
        return dailyPoemDiscussionService.create(resources);
    }

    @PutMapping
    @Log("修改讨论")
    @ApiOperation("修改讨论")
    @AnonymousAccess
    public ResponseEntity<Object> updateDailyPoemDiscussion(@Validated @RequestBody DailyPoemDiscussion resources){
        dailyPoemDiscussionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除讨论")
    @ApiOperation("删除讨论")
    @AnonymousAccess
    public ResponseEntity<Object> deleteDailyPoemDiscussion(@RequestBody List<Integer> ids) {
        dailyPoemDiscussionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PutMapping("/pin/{id}")
    @Log("置顶或取消置顶评论")
    @ApiOperation("置顶或取消置顶评论")
    @AnonymousAccess
    public ResponseEntity<Void> pin(@PathVariable Integer id) {
        dailyPoemDiscussionService.togglePin(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping("/audit/{id}")
    @Log("审核评论")
    @ApiOperation("审核评论")
    @AnonymousAccess
    public ResponseEntity<Void> audit(@PathVariable Integer id,
                                      @RequestParam Integer status) {
        dailyPoemDiscussionService.auditDiscussion(id, status);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
